package com.zahb.qadx.ui.fragment;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.zahb.qadx.R;
import com.zahb.qadx.model.JsonRootMicroVideoBean;
import com.zahb.qadx.model.MainFunc1;
import com.zahb.qadx.model.MainFunc2;
import com.zahb.qadx.net.RetrofitService;
import com.zahb.qadx.ui.activity.videopage.FlipVideoActivity;
import com.zahb.qadx.ui.activity.videopage.MicroVideoListActivity;
import com.zahb.qadx.util.CompatHelper;
import com.zahb.qadx.util.Constant;
import com.zahb.qadx.util.DisplayUtil;
import com.zahb.qadx.util.ImageLoaderKt;
import com.zahb.qadx.util.Tips;

import org.jetbrains.annotations.NotNull;


import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by Zdw on 2020/10/12 17:07
 * 精选视频
 */
public class VideosFragment extends HomeFuncFragment implements OnItemClickListener {

    static final String TAG = "VideosFragment";
    private JsonRootMicroVideoBean classifiedListDataBean;
    private String [] microvideoArray;
        VideosAdapter mVideosAdapter;
   private List<MainFunc2> mainFunc2s;
   private int value=1;
    static VideosFragment newInstance(MainFunc1 mainFunc1) {
        Bundle args = new Bundle();
        args.putSerializable("data", mainFunc1);
        VideosFragment fragment = new VideosFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        transparentCardView();

        mTvTitle.setCompoundDrawablesRelativeWithIntrinsicBounds(
                R.drawable.ic_home_videos, 0, 0, 0);
        MainFunc1 mainFunc1 = (MainFunc1) mArguments.getSerializable("data");

        if (mainFunc1 != null) {



            mTvTitle.setText(mainFunc1.getFunctionName());
            if (mTvTitle.getText().toString().equals("微视频")){
                classifiedListData();
            }

          mainFunc2s = mainFunc1.getInfos();
//            mVideosAdapter.addData(mainFunc2s.size() > 14 ? mainFunc2s.subList(0, 4) : mainFunc2s);//控制首页显示子布局的数量
            mVideosAdapter.addData( mainFunc2s);
            mVideosAdapter.setOnItemClickListener(this);

        }
    }

    @Override
    public void onStart() {
        super.onStart();

    }

    //微视频分类
    private void classifiedListData() {

        addDisposable(RetrofitService.getNetService()
                .getListOfMicroVideoCategories()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    if (response.getStatusCode() == Constant.CODE_SUCCESS) {
                        classifiedListDataBean=response;
//                        for (int i = 0; i <classifiedListDataBean.getData().size() ; i++) {
//
//                            microvideoArray[i]=classifiedListDataBean.getData().get(i).getName();
//
//                        }
                    } else {
                        showBindToast(response.getErrorInfo());
                    }
                }, throwable -> {
                    Tips.RequestError(getActivity());
                    throwable.printStackTrace();

                }));
    }
    @NonNull
    @Override
    protected RecyclerView.LayoutManager getLayoutManager() {
        return new GridLayoutManager(getContext(), 2) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
    }

    @Nullable
    @Override
    protected RecyclerView.ItemDecoration getItemDecoration() {
        final int divider_h = DisplayUtil.dip2px(5.5f);
        final int divider_v = DisplayUtil.dip2px(16.5f);
        return new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view
                    , @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                if (parent.getAdapter() == null) return;
                outRect.set(0, 0, 0, 0);
                if (parent.getChildAdapterPosition(view) % 2 == 0) {// 左边
                    outRect.right = divider_h;
                } else if (parent.getChildAdapterPosition(view) % 2 == 1) {// 右边
                    outRect.left = divider_h;
                }
                if (parent.getChildAdapterPosition(view) >= 2) {
                    outRect.top = divider_v;
                }
            }
        };
    }

    @NonNull
    @Override
    protected RecyclerView.Adapter<BaseViewHolder> getAdapter() {
        mVideosAdapter = new VideosAdapter(R.layout.item_grid_videos);
        return mVideosAdapter;
    }

    @Override
    protected void seeMore() {
//        VideoListActivity.actionStartVideo(getContext());1.3.3视频
//        startActivity(new Intent(getActivity(), MicroVideoListActivity.class));//微视频列表1.3.4
        if (classifiedListDataBean!=null){
            MicroVideoListActivity.actionStart(getContext(), classifiedListDataBean);
        }else {

        }

    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
//        VideoDetailsActivity.actionStart(getContext(), mVideosAdapter.getItem(position), VideoDetailsActivity.TYPE_VIDEO);

        Intent intent = new Intent(getActivity(), FlipVideoActivity.class);
        intent.putExtra("SourceId", mainFunc2s.get(position).getSourceId()+"");
        startActivity(intent);
        getActivity().overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        updateTheNumberOfPlays(mainFunc2s.get(position).getSourceId());
    }
       /*更新微视频播放量
        id:视频id
        */
    private void updateTheNumberOfPlays(int id) {
        addDisposable(RetrofitService.getNetService()
                .getPlayTheRefresh(id)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    hideProgressDialog();
                    if (response.getStatusCode() == Constant.CODE_SUCCESS) {

                    } else {
                        showBindToast(response.getErrorInfo());
                    }
                }, throwable -> {
                    hideProgressDialog();
                    throwable.printStackTrace();
                    Tips.RequestError(getActivity());
                }));
    }

    public static class VideosAdapter extends BaseQuickAdapter<MainFunc2, BaseViewHolder> {

        VideosAdapter(int layoutResId) {
            super(layoutResId);
        }

        @Override
        protected void convert(@NotNull BaseViewHolder baseViewHolder, MainFunc2 mainFunc2) {
            ImageLoaderKt.loadImageSameCorners(baseViewHolder.getView(R.id.iv_icon), mainFunc2.getIcon(), 5f);
            baseViewHolder.setText(R.id.tv_title, mainFunc2.getTitle().trim())
                    .setText(R.id.tv_sub_title, CompatHelper.isEmpty(mainFunc2.getSubTitle())
                            ? "该视频暂无简介" : mainFunc2.getSubTitle());
        }
    }



}
